package com.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 

import com.beans.UserBean;
import com.dao.UserDAO;
/** * Servlet implementation class LoginServlet */ 
public class LoginServlet extends HttpServlet { 
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { 
		try {
			UserBean user = new UserBean(); 
			
			user.setUserName(request.getParameter("un")); 
			user.setPassword(request.getParameter("pw")); 
			
			user = UserDAO.login(user);
			
			
			if (user.isValid() && user.getDept().equals("admin")) {
				HttpSession session = request.getSession(true);
				session.setAttribute("currentSessionUser",user);
				getServletContext().getRequestDispatcher("/WelcomeAdmin.jsp").forward(request, response);//logged-in page
				//response.sendRedirect("WelcomeAdmin.jsp");  
			} else 
				getServletContext().getRequestDispatcher("/error.jsp").forward(request, response);////error page
				//response.sendRedirect("error.jsp"); 
		
		} catch (Throwable theException) { 
			System.out.println(theException); 
		}
	
	}
}
